package cn.schoolwow.ams.module.block.listBlock.daoBlock.entityTable.initial.service;

import cn.schoolwow.ams.domain.block.list.common.ListBlockOption;
import cn.schoolwow.ams.domain.block.list.dao.databaseTable.DatabaseTableListBlockOption;
import cn.schoolwow.ams.domain.block.list.dao.entityTable.EntityTableListBlockConfigAware;
import cn.schoolwow.ams.domain.block.list.dao.entityTable.EntityTableListBlockOption;
import cn.schoolwow.ams.domain.option.QuickAMSListener;
import cn.schoolwow.ams.module.block.listBlock.common.domain.context.ListBlockContext;
import cn.schoolwow.ams.module.block.listBlock.daoBlock.common.domain.DatabaseListBlockContext;
import cn.schoolwow.ams.module.block.listBlock.daoBlock.entityTable.initial.domain.EntityTableListBlockContext;
import cn.schoolwow.ams.module.block.listBlock.daoBlock.entityTable.initial.flow.InitialEntityTableListBlockContextFlow;
import cn.schoolwow.ams.module.initial.domain.InitialAMSContext;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.util.domain.check.instance.CheckInstance;
import org.apache.commons.lang3.Validate;
import org.springframework.context.ApplicationContext;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class InitialEntityTableListBlockListFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        setEntityTableListBlockOptionList(flowContext);
        setEntityTableListBlockContextList(flowContext);
    }

    @Override
    public String name() {
        return "初始化EntityTableListBlock类型页面";
    }

    private void setEntityTableListBlockOptionList(FlowContext flowContext){
        ApplicationContext applicationContext = flowContext.checkInstanceData(ApplicationContext.class);
        Map<String,EntityTableListBlockConfigAware> map = applicationContext.getBeansOfType(EntityTableListBlockConfigAware.class);

        if(map.isEmpty()){
            flowContext.brokenCurrentFlow("无EntityTableListBlock配置类列表信息");
        }
        List<EntityTableListBlockOption> entityTableListBlockOptionList = new ArrayList<>();
        for(EntityTableListBlockConfigAware entityTableListBlockConfigAware:map.values()){
            EntityTableListBlockOption entityTableListBlockOption = entityTableListBlockConfigAware.getAMSBlockConfig();
            entityTableListBlockOption.routerBlock = entityTableListBlockConfigAware;

            String className = entityTableListBlockConfigAware.getClass().getName();
            CheckInstance.newCheck()
                    .notNullCheck(entityTableListBlockOption, "clazz表配置,涉及类名:["+className+"]")
                    .notEmptyCheck(entityTableListBlockOption.projectName, "项目名称,涉及类名:["+className+"]")
                    .notEmptyCheck(entityTableListBlockOption.displayName, "表显示名称,涉及类名:["+className+"]")
                    .notNullCheck(entityTableListBlockOption.daoName, "数据库名称,涉及类名:["+className+"]")
                    .notNullCheck(entityTableListBlockOption.entityClass, "实体类名,涉及类名:["+className+"]");
            entityTableListBlockOptionList.add(entityTableListBlockOption);
        }
        flowContext.putContextData("entityTableListBlockOptionList", entityTableListBlockOptionList);
    }

    private void setEntityTableListBlockContextList(FlowContext flowContext){
        List<EntityTableListBlockOption> entityTableListBlockOptionList = (List<EntityTableListBlockOption>) flowContext.checkData("entityTableListBlockOptionList");
        InitialAMSContext initialAMSContext = flowContext.checkInstanceData(InitialAMSContext.class);
        List<QuickAMSListener> quickAMSListenerList = (List<QuickAMSListener>) flowContext.checkData("quickAMSListenerList");

        List<EntityTableListBlockContext> entityTableListBlockContextList = new ArrayList<>();
        for(EntityTableListBlockOption entityTableListBlockOption:entityTableListBlockOptionList){
            EntityTableListBlockContext entityTableListBlockContext = new EntityTableListBlockContext();
            entityTableListBlockContext.projectName = entityTableListBlockOption.projectName;
            entityTableListBlockContext.displayName = entityTableListBlockOption.displayName;
            entityTableListBlockContext.uniqueKey = entityTableListBlockOption.routerBlock.getBlockUniqueName();
            for(QuickAMSListener quickAMSListener:quickAMSListenerList){
                entityTableListBlockContext.dao = quickAMSListener.getDAO(entityTableListBlockOption.daoName);
                if(null!=entityTableListBlockContext.dao){
                    break;
                }
            }
            Validate.notNull(entityTableListBlockContext.dao, "数据库不存在!数据库名:"+entityTableListBlockOption.daoName);
            entityTableListBlockContext.entity = entityTableListBlockContext.dao.getEntity(entityTableListBlockOption.entityClass);
            Validate.notNull(entityTableListBlockContext.entity, "实体类不存在!实体类名:"+entityTableListBlockOption.entityClass.getName());

            flowContext.startFlow(new InitialEntityTableListBlockContextFlow())
                    .putTemporaryInstanceData(entityTableListBlockOption)
                    .putTemporaryInstanceData(entityTableListBlockContext)
                    .putTemporaryInstanceData(entityTableListBlockOption, ListBlockOption.class)
                    .putTemporaryInstanceData(entityTableListBlockOption, DatabaseTableListBlockOption.class)
                    .putTemporaryInstanceData(entityTableListBlockContext, ListBlockContext.class)
                    .putTemporaryInstanceData(entityTableListBlockContext, DatabaseListBlockContext.class)
                    .execute();

            entityTableListBlockContextList.add(entityTableListBlockContext);
            initialAMSContext.listBlockContextList.add(entityTableListBlockContext);
        }
        flowContext.putTemporaryData("entityTableListBlockContextList", entityTableListBlockContextList);
    }
}
